import 'package:floor/floor.dart';
import 'package:flutter_getx/app/data/db/entity/message.dart';


@dao
abstract class MessageDao {
  @Query('SELECT * FROM MessageEntity')
  Future<List<MessageEntity>> findAllMessages();

  @Query('SELECT * FROM MessageEntity WHERE id = :id')
  Stream<MessageEntity?> findMessageById(int id);
  
  @Query('SELECT * FROM MessageEntity WHERE fromUserId = :uid or toUserId = :uid order by createAt DESC limit 1')
  Future<MessageEntity?> findLastMessageByUserId(int uid);

  @Query('SELECT * FROM MessageEntity WHERE (fromUserId = :fid and toUserId = :uid) or (fromUserId = :uid and toUserId = :fid) order by createAt ASC')
  Future<List<MessageEntity?>> findMessagesByUserId(int fid, int uid);

  @Query('SELECT * FROM MessageEntity WHERE toUserId = :gid order by createAt ASC')
  Future<List<MessageEntity?>> findGroupMessagesByUserId(int gid);

  @insert
  Future<void> insertMessage(MessageEntity message);

  @insert
  Future<List<int>> insertMessages(List<MessageEntity> messages);

  @Update(onConflict: OnConflictStrategy.replace)
  Future<void> updateMessage(MessageEntity message);

  @delete
  Future<void> deleteMessage(MessageEntity message);

  @delete
  Future<int> deleteMessageList(List<MessageEntity> list);

  @Query('delete from MessageEntity')
  Future<void> deleteAll();
}
